<template>
  <div class="container">
    <el-row>
      <el-col :span="20">
        <span style="font-size:18px; font-weight:bold;">品牌管理</span>
      </el-col>
      <el-col style="text-align:right;" :span="4">
        <el-button
          style="margin-left:5px;"
          @click="
            addVisible = true;
            currentBrand = {};
          "
          size="small"
          type="primary"
          >添加</el-button
        >
      </el-col>
    </el-row>
    <el-table
      :data="brandList"
      style="width: 100%;margin-top:5px;"
      :height="tableHeight"
      border
      stripe
    >
      <el-table-column type="index"> </el-table-column>
      <el-table-column prop="zh_name" label="中文名称"></el-table-column>
      <el-table-column prop="en_name" label="英文名称"></el-table-column>
      <el-table-column prop="brand_name" label="缩写"></el-table-column>
      <el-table-column label="图片" width="100" align="center">
        <template slot-scope="scope">
          <img style="width:50px;he" :src="scope.row.brand_img" alt="" />
        </template>
      </el-table-column>
      <el-table-column label="排序">
        <template slot-scope="scope">
          <el-input
            @change="setSort(scope.row)"
            type="number"
            v-model="scope.row.sort"
          >
          </el-input>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="280">
        <template slot-scope="scope">
          <div style="text-align:center;">
            <el-button
              @click="
                addVisible = true;
                currentBrand = scope.row;
              "
              type="primary"
              size="small"
              >编辑</el-button
            >
            <el-button
              @click="deleteBrand(scope.row.id)"
              type="danger"
              size="small"
              >删除</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <add-brand
      :visible="addVisible"
      @close="
        addVisible = false;
        getBrandList();
      "
      :brand="currentBrand"
    ></add-brand>
  </div>
</template>

<script>
import addBrand from "./component/add-brand";
import { getBrandList, setBrandSort,deleteBrand } from "@/api/good-mng";
export default {
  name: "PropMng",
  data() {
    return {
      tableHeight: 0,
      brandList: [],
      kind: [],
      addVisible: false,
      currentBrand: {}
    };
  },
  methods: {
    setSort(row) {
      if (row.sort < 0) {
        this.$message.error("请输入大于等于0的整数");
        return;
      }
      setBrandSort({
        id: row.id,
        sort: row.sort
      }).then(res => {
        if (res.code === 200) {
          this.getBrandList();
        }
      });
    },
    deleteBrand(id) {
      this.$confirm("确认删除该品牌?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        deleteBrand({ id }).then(res => {
          if (res.code === 200) {
            this.getBrandList();
            this.$message({
              type: "success",
              message: "删除成功!"
            });
          }
        });
      });
    },
    changeSize() {
      this.tableHeight = document.body.clientHeight - 150;
      window.onresize = () => {
        this.changeSize();
      };
    },
    getBrandList() {
      getBrandList().then(res => {
        if (res.code === 200) {
          this.brandList = res.data;
        }
      });
    }
  },
  created() {
    this.changeSize();
    this.getBrandList();
  },
  components: {
    addBrand
  }
};
</script>

<style>
</style>
